% by clq ylx
% 2021/6/12
%% added timing function 
%% passed test
function [best_is_pref,opt_profits_pref] = prop_pref(j_all, pref)
    addpath functions;
    global I J P CURRENT

    Line = j_all;
    patient_number = sum(Line > 0); % equivalent to size(2)
    S_pref = zeros(I, J);

    best_is_pref = zeros(1, patient_number);
    opt_profits_pref = zeros(1,patient_number);
    Q = ones(I,1);
    for n = 1:patient_number
        CURRENT=n;
        Q = [Q, zeros(I,1)];
        j = Line(n);
        profits = zeros(1, I);
        for i = 1:I
            Q_test = findQ2(Q,i,j,P,n); 
            R_test = findR(Q_test);
            profits(i) = function_fQR2(Q_test, R_test);
        end
    % above is the same with proposed main without preference    

    %% special part for proposed pref
        
        a=(1+(pref(n)-1)*I/2);b=pref(n)*I/2; %replaced the if else condition
        profit_temp=max(profits(a:b));
        i=find(profits==profit_temp); % assign to the best slot according to preference
      
        best_is_pref(n) = i;
        S_pref(i, j) = S_pref(i, j) + 1;
        Q = findQ2(Q,i,j,P,n);
        R = findR(Q);
        opt_profits_pref(n) = profits(i); % find the optimal profit
        % fprintf("    Time = %.2f seconds \n\n", ftimer()-start_time);
    end
